package com.Txunda.fxw.ui.activity.carve;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.L;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.other.PathAty;
import com.Txunda.fxw.ui.adapter.Law_DetailAdapter;
import com.Txunda.fxw.ui.adapter.model.LawModel;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.Syb;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.view.RoundedImageView;
import com.Txunda.fxw.view.dialog.AlertDialog;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

@SuppressLint("NewApi")
public class CarveDetailsAty extends BaseAty {
    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.tv_carve_recycler)
    RecyclerView tvCarveRecycler;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.img_company_pic)
    ImageView imgCompanyPic;
    @BindView(R.id.tv_company_title)
    TextView tvCompanyTitle;
    @BindView(R.id.fl_company)
    FrameLayout flCompany;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_one_phone)
    LinearLayout llOnePhone;
    @BindView(R.id.tv_one_address)
    TextView tvOneAddress;
    @BindView(R.id.ll_one_address)
    RelativeLayout llOneAddress;
    @BindView(R.id.tv_carve_work_one)
    TextView tvCarveWorkOne;
    @BindView(R.id.tv_carve_work_two)
    TextView tvCarveWorkTwo;
    @BindView(R.id.ll_carve_one)
    LinearLayout llCarveOne;
    @BindView(R.id.tv_stocr_right)
    TextView tvStocrRight;
    @BindView(R.id.tv_direction)
    TextView tvDirection;
    @BindView(R.id.tv_consultation_phone)
    TextView tvConsultationPhone;
    @BindView(R.id.ll_two_phone)
    LinearLayout llTwoPhone;
    @BindView(R.id.tv_two_address)
    TextView tvTwoAddress;
    @BindView(R.id.ll_two_address)
    RelativeLayout llTwoAddress;
    @BindView(R.id.tv_guarantee_institution)
    TextView tvGuaranteeInstitution;
    @BindView(R.id.tv_carve_two_work_one)
    TextView tvCarveTwoWorkOne;
    @BindView(R.id.tv_carve_two_work_two)
    TextView tvCarveTwoWorkTwo;
    @BindView(R.id.ll_carve_two)
    LinearLayout llCarveTwo;
    @BindView(R.id.tv_management_idea)
    TextView tvManagementIdea;
    @BindView(R.id.tv_three_phone)
    TextView tvThreePhone;
    @BindView(R.id.ll_three_phone)
    LinearLayout llThreePhone;
    @BindView(R.id.tv_three_address)
    TextView tvThreeAddress;
    @BindView(R.id.ll_three_address)
    RelativeLayout llThreeAddress;
    @BindView(R.id.tv_financing_information)
    TextView tvFinancingInformation;
    @BindView(R.id.tv_three_guarantee_institution_one)
    TextView tvThreeGuaranteeInstitutionOne;
    @BindView(R.id.tv_three_guarantee_institution_two)
    TextView tvThreeGuaranteeInstitutionTwo;
    @BindView(R.id.ll_carve_three)
    LinearLayout llCarveThree;
    @BindView(R.id.img_pic)
    ImageView imgPic;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.fl_img)
    FrameLayout flImg;
    @BindView(R.id.tv_name_wechat)
    TextView tvNameWechat;
    @BindView(R.id.linear_more_wechat)
    RelativeLayout linearMoreWechat;
    @BindView(R.id.open_side_iv)
    RoundedImageView openSideIv;
    @BindView(R.id.side_bar_phone_tv)
    LinearLayout sideBarPhoneTv;
    @BindView(R.id.side_bar_like_iv)
    ImageView sideBarLikeIv;
    @BindView(R.id.side_bar_like_tv)
    TextView sideBarLikeTv;
    @BindView(R.id.side_bar_like_layout)
    LinearLayout sideBarLikeLayout;
    @BindView(R.id.side_bar_evaluate_tv)
    LinearLayout sideBarEvaluateTv;
    @BindView(R.id.side_bar_settings_tv)
    LinearLayout sideBarSettingsTv;
    @BindView(R.id.side_bar_share_tv)
    LinearLayout sideBarShareTv;
    @BindView(R.id.app_side_top_layout)
    LinearLayout appSideTopLayout;
    @BindView(R.id.linear_share2)
    LinearLayout linearShare2;
    @BindView(R.id.linear_weibo)
    LinearLayout linearWeibo;
    @BindView(R.id.linear_weixin)
    LinearLayout linearWeixin;
    @BindView(R.id.linear_friends)
    LinearLayout linearFriends;
    @BindView(R.id.linear_qq)
    LinearLayout linearQq;
    @BindView(R.id.linear_bottom)
    LinearLayout linearBottom;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private Law_DetailAdapter detail_adapter;

    private List<LawModel> llist_data;

    private String lat;
    private String lnt;
    private String company_name;
    private String pic;
    private String address;

    private String url;
    private String wechat_url;
    private String tea_id;
    private String syb_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        String district_name = getIntent().getStringExtra("district_name");
        if (district_name == null || district_name.equals("")) {
            atyTitle3TvRight2.setText(area_name);
        } else {
            atyTitle3TvRight2.setText(district_name);
        }
        district_name = getIntent().getStringExtra("district_name");
        if (district_name == null || district_name.equals("")) {
            atyTitle3TvRight2.setText(area_name);
        } else {
            atyTitle3TvRight2.setText(district_name);
        }

        setDrawerLayout(drawerLayout);
        getLikesId(sideBarLikeIv, syb_id);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Log.d("zdl", "===========获取到的数据============="+jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("Syb/sybInfo")) { // 商家详情
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data))
                return;
            if ("1".equals(tea_id)) {
                // 加载数据
                String is_coffee = data.get("is_coffe"); // 1.有咖啡
                String is_tea_water = data.get("is_tea_water"); // 1有茶水
                String is_fallow = data.get("is_fallow"); // 1有休闲
                String is_consultant = data.get("is_consultant");// 1有咨询
                String is_shop = data.get("is_shop"); // 1能购物

                LawModel lawModel;

                if ("1".equals(is_coffee)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_detail_coffee);
                    lawModel.setTitle("咖啡");
                    llist_data.add(lawModel);
                }

                if ("1".equals(is_tea_water)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_detail_tea_drink);
                    lawModel.setTitle("茶水");
                    llist_data.add(lawModel);
                }
                if ("1".equals(is_fallow)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_detail_game);
                    lawModel.setTitle("休闲");
                    llist_data.add(lawModel);
                }
                if ("1".equals(is_consultant)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_zixun);
                    lawModel.setTitle("咨询");
                    llist_data.add(lawModel);
                }
                if ("1".equals(is_shop)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_shopping);
                    lawModel.setTitle("购物");
                    llist_data.add(lawModel);
                }
                detail_adapter.setList(llist_data);
                tvCarveRecycler.setAdapter(detail_adapter);

                tvService.setText(data.get("service_tenet"));
                tvTime.setText(data.get("shop_hours"));
                tvPhone.setText(data.get("tel"));
                tvOneAddress.setText(data.get("address"));
                tvCarveWorkOne.setText(data.get("brand_content_url1"));
                tvCarveWorkTwo.setText(data.get("brand_content_url2"));
                Glide.with(this).load(data.get("pic")).into(imgPic);
                tvTitle.setText(data.get("title"));
                tvNameWechat.setText(data.get("wechat"));
            } else if ("2".equals(tea_id)) {
                // 加载数据
                String is_projectint = data.get("is_projectint"); // 1.有项目投资
                String is_intmanagement = data.get("is_intmanagement"); // 1有投资管理
                String is_intconsultant = data.get("is_intconsultant"); // 1有投资咨询
                String id_equityint = data.get("id_equityint");// 1有股权投资
                LawModel lawModel;
                if ("1".equals(is_projectint)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_new_xiangmutouzi);
                    lawModel.setTitle("投资项目");
                    llist_data.add(lawModel);
                }

                if ("1".equals(is_intmanagement)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_touziguanli);
                    lawModel.setTitle("投资管理");
                    llist_data.add(lawModel);
                }
                if ("1".equals(is_intconsultant)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_touzizixun);
                    lawModel.setTitle("投资咨询");
                    llist_data.add(lawModel);
                }
                if ("1".equals(id_equityint)) {
                    lawModel = new LawModel();
                    lawModel.setImg(R.drawable.icon_new_guquantouzi);
                    lawModel.setTitle("股权投资");
                    llist_data.add(lawModel);
                }
                detail_adapter.setList(llist_data);
                tvCarveRecycler.setAdapter(detail_adapter);
                tvDirection.setText(data.get("int_direction"));
                tvConsultationPhone.setText(data.get("tel"));
                tvTwoAddress.setText(data.get("address"));
                tvGuaranteeInstitution.setText(data.get("product"));
                tvCarveTwoWorkOne.setText(data.get("brand_content_url1"));
                tvCarveTwoWorkTwo.setText(data.get("brand_content_url2"));
                tvNameWechat.setText(data.get("wechat"));
                Glide.with(this).load(data.get("pic")).into(imgPic);
                tvTitle.setText(data.get("title"));
            } else if ("3".equals(tea_id)) {
                tvCompanyName.setText(data.get("company"));
                Glide.with(this).load(data.get("pic")).into(imgCompanyPic);
                tvCompanyTitle.setText(data.get("title"));
                tvManagementIdea.setText(data.get("idea"));
                tvThreePhone.setText(data.get("tel"));
                tvThreeAddress.setText(data.get("address"));
                tvFinancingInformation.setText(data.get("brand_content"));
                tvThreeGuaranteeInstitutionOne.setText(data.get("brand_content_url1"));
                tvThreeGuaranteeInstitutionTwo.setText(data.get("brand_content_url2"));
            }
            wechat_url = data.get("wechat_url");
            address = data.get("address");
            lat = data.get("lat"); // 纬度
            lnt = data.get("lnt"); // 经度
            company_name = data.get("name"); // 商家名称
            pic = data.get("pic");
            url = data.get("url");
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_carve_details;
    }

    @Override
    protected void initialized() {
        syb_id = getIntent().getStringExtra("syb_id");
        type = "q" + syb_id;
        Syb syb = new Syb();
        syb.sybInfo(syb_id, this);


        llist_data = new ArrayList<>();
        String name = getIntent().getStringExtra("name");
        tea_id = getIntent().getStringExtra("tea_id");
        if ("1".equals(tea_id)) { // 投融服务创业平台
            atyTitle3TvCenter.setText(name);
            tvCarveRecycler.setVisibility(View.VISIBLE);
            llCompany.setVisibility(View.GONE);
            llCarveOne.setVisibility(View.VISIBLE);
            llCarveTwo.setVisibility(View.GONE);
            llCarveThree.setVisibility(View.GONE);
            flImg.setVisibility(View.VISIBLE);
            linearMoreWechat.setVisibility(View.VISIBLE);
            flCompany.setVisibility(View.GONE);
        } else if ("2".equals(tea_id)) { // 投资人
            atyTitle3TvCenter.setText(name);
            tvCarveRecycler.setVisibility(View.VISIBLE);
            llCompany.setVisibility(View.GONE);
            llCarveOne.setVisibility(View.GONE);
            llCarveTwo.setVisibility(View.VISIBLE);
            llCarveThree.setVisibility(View.GONE);
            flImg.setVisibility(View.VISIBLE);
            linearMoreWechat.setVisibility(View.VISIBLE);
            flCompany.setVisibility(View.GONE);
        } else if ("3".equals(tea_id)) {// 融资人
            atyTitle3TvCenter.setText(name);
            tvCarveRecycler.setVisibility(View.GONE);
            llCompany.setVisibility(View.VISIBLE);
            llCarveOne.setVisibility(View.GONE);
            llCarveTwo.setVisibility(View.GONE);
            llCarveThree.setVisibility(View.VISIBLE);
            flImg.setVisibility(View.GONE);
            linearMoreWechat.setVisibility(View.GONE);
            flCompany.setVisibility(View.VISIBLE);
        }

        // 动画
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

        atyTitle3TvRight2.setVisibility(View.VISIBLE);
        // 设置LinearLayoutManager
        tvCarveRecycler.setLayoutManager(
                new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        tvCarveRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        tvCarveRecycler.setHasFixedSize(true);

        detail_adapter = new Law_DetailAdapter(CarveDetailsAty.this);
    }

    @Override
    @OnClick({R.id.linear_more_wechat, R.id.ll_one_address, R.id.ll_one_phone,
            R.id.img_pic, R.id.ll_two_address, R.id.ll_two_phone,
            R.id.ll_three_phone, R.id.ll_three_address, R.id.img_company_pic,
            R.id.open_side_iv, R.id.side_bar_share_tv, R.id.linear_share2})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.open_side_iv:// 侧边栏
                if (!drawerLayout.isDrawerOpen(Gravity.END))
                    drawerLayout.openDrawer(Gravity.END);
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            case R.id.side_bar_share_tv:
                appSideTopLayout.setVisibility(View.GONE);
                linearBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.linear_share2:
                appSideTopLayout.setVisibility(View.VISIBLE);
                linearBottom.setVisibility(View.GONE);
                break;
            case R.id.ll_three_phone:
                try {
                    new AlertDialog(this).builder().setTitle("提示").setMsg("tel: " + tvThreePhone.getText().toString())
                            .setPositiveButton("确认", new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL,
                                            Uri.parse("tel: " + tvThreePhone.getText().toString()));
                                    startActivity(intent);

                                }
                            }).setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.ll_two_phone:
                try {
                    new AlertDialog(this).builder().setTitle("提示")
                            .setMsg("tel: " + tvConsultationPhone.getText().toString())
                            .setPositiveButton("确认", new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL,
                                            Uri.parse("tel: " + tvConsultationPhone.getText().toString()));
                                    startActivity(intent);

                                }
                            }).setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.ll_one_phone:
                try {
                    new AlertDialog(this).builder().setTitle("提示").setMsg("tel: " + tvPhone.getText().toString())
                            .setPositiveButton("确认", new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_CALL,
                                            Uri.parse("tel: " + tvPhone.getText().toString()));
                                    startActivity(intent);

                                }
                            }).setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.linear_more_wechat:
                try {
                    Uri uri = Uri.parse(wechat_url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.ll_two_address:
                Bundle bundle = new Bundle();
                bundle.putString("lat", lat);
                bundle.putString("lnt", lnt);
                bundle.putString("name", company_name); // 商家名称
                bundle.putString("address", address);
                bundle.putString("pic", pic);
                startActivity(PathAty.class, bundle);
                break;
            case R.id.ll_three_address:
                bundle = new Bundle();
                bundle.putString("lat", lat);
                bundle.putString("lnt", lnt);
                bundle.putString("name", company_name); // 商家名称
                bundle.putString("address", address);
                bundle.putString("pic", pic);
                startActivity(PathAty.class, bundle);
                break;
            case R.id.ll_one_address: // 地址
                bundle = new Bundle();
                bundle.putString("lat", lat);
                bundle.putString("lnt", lnt);
                bundle.putString("name", company_name); // 商家名称
                bundle.putString("address", address);
                bundle.putString("pic", pic);
                startActivity(PathAty.class, bundle);
                break;
            case R.id.img_pic: // 图片链接
                try {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    L.e(e.getMessage());
                }
                break;
            case R.id.img_company_pic:
                try {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    L.e(e.getMessage());
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void requestData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
